package com.tanhua.houtai.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.common.pojo.UserInfo;
import com.tanhua.houtai.enums.PublishStateEnum;
import com.tanhua.houtai.mapper.PublishInfoMapper;
import com.tanhua.houtai.pojo.Publish;
import com.tanhua.houtai.pojo.PublishInfo;
import com.tanhua.houtai.vo.ItemVo;
import com.tanhua.houtai.vo.PagerVo;
import com.tanhua.houtai.vo.TotalVo;

import com.tanhua.server.service.UserInfoService;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DynamicVerifiesService {

    @Autowired
    private PublishInfoMapper publishInfoMapper;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private UserInfoService userInfoService;

    public PagerVo messageflipping(Integer pageSize, Integer page) {
        PagerVo pagerVO = new PagerVo();//将要返回的数据
        pagerVO.setPagesize(pageSize);//设置分页大小
        pagerVO.setPage(page);//设置当前页码
        QueryWrapper<PublishInfo> queryWrapper = new QueryWrapper();
        queryWrapper.select("id");
        Integer count = publishInfoMapper.selectCount(queryWrapper);
        pagerVO.setCounts(count);//设置总条数

        List<TotalVo> totalVoList = new ArrayList<>();
        //状态合计 全部
        TotalVo totalVo1 = new TotalVo();
        totalVo1.setTitle("全部");  //设置标题
        totalVo1.setCode("all");      //设置状态码
        QueryWrapper<PublishInfo> queryWrapper1 = new QueryWrapper();
        queryWrapper1.select("state");
        Integer value = publishInfoMapper.selectCount(queryWrapper1);
        totalVo1.setValue(value); //设置总数

        totalVoList.add(totalVo1);
        //状态合计 待审核
        TotalVo totalVo2 = new TotalVo();
        totalVo2.setTitle("待审核");
        totalVo2.setCode("3");
        QueryWrapper<PublishInfo> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("state", "3");
        Integer value2 = publishInfoMapper.selectCount(queryWrapper2);
        totalVo2.setValue(value2);

        totalVoList.add(totalVo2);

        //状态合计 已通过
        TotalVo totalVo3 = new TotalVo();
        totalVo3.setTitle("已通过");
        totalVo3.setCode("4");
        QueryWrapper<PublishInfo> queryWrapper3 = new QueryWrapper<>();
        queryWrapper3.eq("state", "4");
        Integer value3 = publishInfoMapper.selectCount(queryWrapper3);
        totalVo3.setValue(value3);

        totalVoList.add(totalVo3);

        //状态合计 已驳回
        TotalVo totalVo4 = new TotalVo();
        totalVo4.setTitle("已驳回");
        totalVo4.setCode("5");
        QueryWrapper<PublishInfo> queryWrapper4 = new QueryWrapper<>();
        queryWrapper4.eq("state", "5");
        Integer value4 = publishInfoMapper.selectCount(queryWrapper4);
        totalVo4.setValue(value4);

        totalVoList.add(totalVo4);
        pagerVO.setTotals(totalVoList); //设置状态合计


        List<ItemVo> itemVoList = new ArrayList<>();

        //所有动态审核数据列表
        List<PublishInfo> publishInfos = publishInfoMapper.selectList(null);

        List<Publish> allPublish = mongoTemplate.find(new Query().limit(100), Publish.class);
        int size = allPublish.size();


        int idCount = 0;

        for (PublishInfo publishInfo : publishInfos) {
            if (++idCount > 100) {
                break;
            }
            //用户id
            Integer uid = publishInfo.getUserId();

            UserInfo userInfo = userInfoService.queryUserInfoByUserId(Long.valueOf(uid));


            //点赞数
            Integer likeCount = publishInfo.getLikeCount();
            Integer commentCount = publishInfo.getCommentCount();
            Integer forwardingCount = publishInfo.getForwardingCount();
            Integer reportCount = publishInfo.getReportCount();
            Long created = publishInfo.getCreated();
            //动态id
            String publishid = publishInfo.getPublishId();


            ItemVo itemVo = new ItemVo();
            itemVo.setLikeCount(likeCount);
            itemVo.setCommentCount(commentCount);
            itemVo.setForwardingCount(forwardingCount);
            itemVo.setReportCount(reportCount);
            itemVo.setCreateDate(created.intValue());  //转换为Integer
            itemVo.setId(publishid);

            switch (publishInfo.getState()) {
                case 1:
                    itemVo.setState(PublishStateEnum.DAIJISHENG.toString());
                    break;

                case 2:
                    itemVo.setState(PublishStateEnum.JPASS.toString());
                    break;

                case 3:
                    itemVo.setState(PublishStateEnum.DAIRENSHENG.toString());
                    break;

                case 4:
                    itemVo.setState(PublishStateEnum.RFAIL.toString());
                    break;

                case 5:
                    itemVo.setState(PublishStateEnum.RPASS.toString());
                    break;

                case 6:
                    itemVo.setState(PublishStateEnum.JFAIL.toString());
                    break;
            }


            //用户信息
            itemVo.setNickname(userInfo.getNickName());
            itemVo.setUserLog(userInfo.getLogo());
            itemVo.setUserId(uid);

            //随机生成下标
            int randomPublishId = RandomUtils.nextInt(0, size - 1);


            Publish publish = allPublish.get(randomPublishId);

            if (StringUtils.isNotEmpty(publish.getText())) {
                itemVo.setText(publish.getText());
            } else {
                itemVo.setText("");
            }

            itemVo.setMedias(publish.getMedias().toArray(new String[0]));

            itemVoList.add(itemVo);
        }

        pagerVO.setItems(itemVoList);


        return pagerVO;
    }
}
